package com.baidu.leetcode.controller;


import com.baidu.leetcode.test.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {
    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private TestService testService;

    @RequestMapping("/test")
    @ResponseBody
    public String test(@RequestParam("name") String name) {
        Test test = null;
        try {
            test = testService.findByName(name);
        } catch (Exception e) {
            test = new Test();
            logger.error("test error.", e);
        }
        logger.info("test....{}", name);
        return test.toString();
    }

    @RequestMapping("/save")
    @ResponseBody
    public String saveTest(@RequestParam("name") String name) {
        String res = "0";
        try {
            testService.saveTest(name);
        } catch (Exception e) {
            res = "1";
            logger.error("save test error.", e);
        }
        logger.info("save test....{}", name);
        return res;
    }

    @GetMapping("query")
    @ResponseBody
    public void query(@RequestParam("type") String type){
        Factory.doPro(type).doEvent(type);
    }
}
